<?php
/**
 * Created by Jasmine.
 * Date: 2016/9/23
 * description:
 * Address: 浙江宣逸网络科技有限公司
 */

namespace Admin\Controller;


use Admin\BaseController;
use Common\Model\EditorModel;
use Think\Page;

class EditorController extends BaseController{
    //编辑日数据
    public function editorDayData(){
        $startDate = I('get.startDate','');
        $endDate = I('get.endDate','');
        $type = I('get.editType',0,'intval');
        $where = "1";

        if($type){
            $where .= " and type= {$type}";
        }
        if($startDate){
            $startTime = strtotime($startDate);
            $startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
            $where .= " and addTime >= $startTime";
        }

        if($endDate){
            $endTime = strtotime($endDate);
            $endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
            $where .= " and addTime <= $endTime";
        }
        $count = M('editor_day_data',null,DB_MAIN_CFG)->where($where)->count();
        $this->smarty->assign('totalDataCount',$count);

        $limit = I('get.limit',10,'int');
        $Page = new Page($count,$limit);
        fetchPage($this->smarty,$Page);

        //查询数据
        $data = M('editor_day_data',null,DB_MAIN_CFG)
            ->where($where)
            ->order("objTime DESC,type ASC")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        foreach($data as $key => $val){
            $data[$key]['type'] = EditorModel::getType($val['type'])[0];
        }
        $this->smarty->assign('data',$data);

        $editType = M('cfg_content_type',null,DB_MAIN_CFG)->select();
        $this->smarty->assign('editType',$editType);
        $this->smarty->assign('type',$type);
        $this->smarty->assign('startDate',$startDate);
        $this->smarty->assign('endDate',$endDate);
        $this->smarty->assign('limit',$limit);
        $this->smarty->assign('title',"编辑数据统计-日数据");
        $this->smarty->display('Editor/EditorData/dayData.html');
    }

    //编辑周数据
    public function editorWeekData(){
        $weekDate = I('get.weekDate','');
        $type = I('get.editType',0,'intval');
        $where = "1";

        if($type){
            $where .= " and type= {$type}";
        }
        $time = strtotime($weekDate);
        if($time){
            $beginWeekTime=mktime(0,0,0,date('m',$time),date('d',$time)-date('w',$time)+1,date('Y',$time));
            $endWeekTime=mktime(23,59,59,date('m',$time),date('d',$time)-date('w',$time)+7,date('Y',$time));
            $where .=" and objTime between {$beginWeekTime} and {$endWeekTime}";
        }

        $count = M('editor_week_data',null,DB_MAIN_CFG)->where($where)->count();
        $this->smarty->assign('totalDataCount',$count);

        $limit = I('get.limit',10,'int');
        $Page = new Page($count,$limit);
        fetchPage($this->smarty,$Page);

        //查询数据
        $data = M('editor_week_data',null,DB_MAIN_CFG)
            ->where($where)
            ->order("objTime DESC,type ASC")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        foreach($data as $key => $val){
            $data[$key]['weekBeginTime'] = mktime(0,0,0,date('m',$val['objTime']),date('d',$val['objTime'])-date('w',$val['objTime'])+1,date('Y',$val['objTime']));
            $data[$key]['weekEndTime'] = mktime(23,59,59,date('m',$val['objTime']),date('d',$val['objTime'])-date('w',$val['objTime'])+7,date('Y',$val['objTime']));
            $data[$key]['type'] = EditorModel::getType($val['type'])[0];
        }
        $this->smarty->assign('data',$data);

        $editType = M('cfg_content_type',null,DB_MAIN_CFG)->select();
        $this->smarty->assign('editType',$editType);

        $this->smarty->assign('type',$type);
        $this->smarty->assign('weekDate',$weekDate);
        $this->smarty->assign('limit',$limit);
        $this->smarty->assign('title',"编辑数据统计-周数据");
        $this->smarty->display('Editor/EditorData/weekData.html');
    }

    //编辑月数据
    public function editorMonthData(){
        $monDate = I('get.monDate','');
        $type = I('get.editType',0,'intval');
        $where = "1";

        if($type){
            $where .= " and type= {$type}";
        }
        $time = strtotime($monDate);
        if($time){
            $beginThisMonth=mktime(0,0,0,date('m'),1,date('Y'));
            $endThisMonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
            $where .=" and objTime between {$beginThisMonth} and {$endThisMonth}";
        }

        $count = M('editor_month_data',null,DB_MAIN_CFG)->where($where)->count();
        $this->smarty->assign('totalDataCount',$count);

        $limit = I('get.limit',10,'int');
        $Page = new Page($count,$limit);
        fetchPage($this->smarty,$Page);

        //查询数据
        $data = M('editor_month_data',null,DB_MAIN_CFG)
            ->where($where)
            ->order("objTime DESC,type ASC")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        foreach($data as $key => $val){
            $data[$key]['type'] = EditorModel::getType($val['type'])[0];
        }
        $this->smarty->assign('data',$data);

        $editType = M('cfg_content_type',null,DB_MAIN_CFG)->select();
        $this->smarty->assign('editType',$editType);

        $this->smarty->assign('type',$type);
        $this->smarty->assign('monDate',$monDate);
        $this->smarty->assign('limit',$limit);
        $this->smarty->assign('title',"编辑数据统计-月数据");
        $this->smarty->display('Editor/EditorData/monthData.html');
    }

    //数据排行
    public function editorDataRank(){
        $monDate = I('get.monDate','');
        $where = "1";

        $time = strtotime($monDate);
        if($time){
            $beginThisMonth=mktime(0,0,0,date('m'),1,date('Y'));
            $endThisMonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
            $where .=" and objTime between {$beginThisMonth} and {$endThisMonth}";
        }

        $count = M('editor_data_rank',null,DB_MAIN_CFG)->where($where)->count();
        $this->smarty->assign('totalDataCount',$count);

        $limit = I('get.limit',10,'int');
        $Page = new Page($count,$limit);
        fetchPage($this->smarty,$Page);

        //查询数据
        $data = M('editor_data_rank',null,DB_MAIN_CFG)
            ->where($where)
            ->order("addArticleNum DESC")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        foreach($data as $key => $val){
            $data[$key]['type'] = EditorModel::getType($val['type'])[0];
        }
        $this->smarty->assign('data',$data);
        $this->smarty->assign('monDate',$monDate);
        $this->smarty->assign('limit',$limit);
        $this->smarty->assign('title',"编辑数据统计-数据排行");
        $this->smarty->display('Editor/EditorData/dataRank.html');
    }

    //达人日数据
    public function daRenDayData(){
        $startDate = I('get.startDate','');
        $endDate = I('get.endDate','');
        $type = I('get.type',0,'intval');
        $where = "1";

        if($type){
            $where .= " and type= {$type}";
        }
        if($startDate){
            $startTime = strtotime($startDate);
            $startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
            $where .= " and addTime >= $startTime";
        }

        if($endDate){
            $endTime = strtotime($endDate);
            $endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
            $where .= " and addTime <= $endTime";
        }

        $count = M('expert_day_data',null,DB_MAIN_CFG)->where($where)->count();
        $this->smarty->assign('totalDataCount',$count);

        $limit = I('get.limit',10,'int');
        $Page = new Page($count,$limit);
        fetchPage($this->smarty,$Page);

        //查询数据
        $data = M('expert_day_data',null,DB_MAIN_CFG)
            ->where($where)
            ->order("objTime DESC,type ASC")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        foreach($data as $key => $val){
            $data[$key]['type'] = EditorModel::getType($val['type'])[0];
        }
        $this->smarty->assign('data',$data);

        $editType = M('cfg_content_type',null,DB_MAIN_CFG)->select();
        $this->smarty->assign('editType',$editType);

        $this->smarty->assign('type',$type);
        $this->smarty->assign('startDate',$startDate);
        $this->smarty->assign('endDate',$endDate);
        $this->smarty->assign('limit',$limit);
        $this->smarty->assign('title',"达人数据统计-日数据");
        $this->smarty->display('Editor/DaRenData/dayData.html');
    }

    //达人周数据
    public function daRenWeekData(){
        $weekDate = I('get.weekDate','');
        $type = I('get.type',0,'intval');
        $where = "1";

        if($type){
            $where .= " and type= {$type}";
        }
        $time = strtotime($weekDate);
        if($time){
            $beginWeekTime=mktime(0,0,0,date('m',$time),date('d',$time)-date('w',$time)+1,date('Y',$time));
            $endWeekTime=mktime(23,59,59,date('m',$time),date('d',$time)-date('w',$time)+7,date('Y',$time));
            $where .=" and objTime between {$beginWeekTime} and {$endWeekTime}";
        }

        $count = M('expert_week_data',null,DB_MAIN_CFG)->where($where)->count();
        $this->smarty->assign('totalDataCount',$count);

        $limit = I('get.limit',10,'int');
        $Page = new Page($count,$limit);
        fetchPage($this->smarty,$Page);

        //查询数据
        $data = M('expert_week_data',null,DB_MAIN_CFG)
            ->where($where)
            ->order("objTime DESC,type ASC")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        foreach($data as $key => $val){
            $data[$key]['weekBeginTime'] = mktime(0,0,0,date('m',$val['objTime']),date('d',$val['objTime'])-date('w',$val['objTime'])+1,date('Y',$val['objTime']));
            $data[$key]['weekEndTime'] = mktime(23,59,59,date('m',$val['objTime']),date('d',$val['objTime'])-date('w',$val['objTime'])+7,date('Y',$val['objTime']));
            $data[$key]['type'] = EditorModel::getType($val['type'])[0];
        }
        $this->smarty->assign('data',$data);

        $editType = M('cfg_content_type',null,DB_MAIN_CFG)->select();
        $this->smarty->assign('editType',$editType);
        $this->smarty->assign('type',$type);
        $this->smarty->assign('weekDate',$weekDate);
        $this->smarty->assign('limit',$limit);
        $this->smarty->assign('title',"达人数据统计-周数据");
        $this->smarty->display('Editor/DaRenData/weekData.html');
    }

    //达人月数据
    public function daRenMonthData(){
        $monDate = I('get.monDate','');
        $type = I('get.type',0,'intval');
        $where = "1";

        if($type){
            $where .= " and type= {$type}";
        }
        $time = strtotime($monDate);
        if($time){
            $beginThisMonth=mktime(0,0,0,date('m'),1,date('Y'));
            $endThisMonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
            $where .=" and objTime between {$beginThisMonth} and {$endThisMonth}";
        }

        $count = M('expert_month_data',null,DB_MAIN_CFG)->where($where)->count();
        $this->smarty->assign('totalDataCount',$count);

        $limit = I('get.limit',10,'int');
        $Page = new Page($count,$limit);
        fetchPage($this->smarty,$Page);

        //查询数据
        $data = M('expert_month_data',null,DB_MAIN_CFG)
            ->where($where)
            ->order("objTime DESC,type ASC")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        foreach($data as $key => $val){
            $data[$key]['type'] = EditorModel::getType($val['type'])[0];
        }
        $this->smarty->assign('data',$data);

        $editType = M('cfg_content_type',null,DB_MAIN_CFG)->select();
        $this->smarty->assign('editType',$editType);
        $this->smarty->assign('type',$type);
        $this->smarty->assign('monDate',$monDate);
        $this->smarty->assign('limit',$limit);
        $this->smarty->assign('title',"达人数据统计-月数据");
        $this->smarty->display('Editor/DaRenData/monthData.html');
    }

    //达人数据排行
    public function daRenDataRank(){
        $monDate = I('get.monDate','');
        $where = "1";
        $time = strtotime($monDate);
        if($time){
            $beginThisMonth=mktime(0,0,0,date('m'),1,date('Y'));
            $endThisMonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
            $where .=" and objTime between {$beginThisMonth} and {$endThisMonth}";
        }

        $count = M('expert_data_rank',null,DB_MAIN_CFG)->where($where)->count();
        $this->smarty->assign('totalDataCount',$count);

        $limit = I('get.limit',10,'int');
        $Page = new Page($count,$limit);
        fetchPage($this->smarty,$Page);

        //查询数据
        $data = M('expert_data_rank',null,DB_MAIN_CFG)
            ->where($where)
            ->order("addArticleNum DESC")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        foreach($data as $key => $val){
            $data[$key]['type'] = EditorModel::getType($val['type'])[0];
        }
        $this->smarty->assign('data',$data);

        $this->smarty->assign('limit',$limit);
        $this->smarty->assign('title',"达人数据统计-数据排行");
        $this->smarty->display('Editor/DaRenData/dataRank.html');
    }

    //商务评价
    public function evaluate(){
        $startDate = I('get.startDate','');
        $endDate = I('get.endDate','');
        $type = I('get.type',0,'intval');
        $keyword = I('get.keyword',null,'addslashes');
        $where="1 and status=".EditorModel::COMMERCE_EVALUATE_ON;
        if($startDate){
            $startTime = strtotime($startDate);
            $startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
            $where .= " and addTime >= $startTime";
        }

        if($endDate){
            $endTime = strtotime($endDate);
            $endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
            $where .= " and addTime <= $endTime";
        }

        if($type){
            $where .= " and objType= {$type}";
        }
        if($keyword){
            $where .= " and editorName like '%{$keyword}%' or commerceName like '%{$keyword}%'";
        }

        $count = M('commerce_evaluate',null,DB_MAIN_CFG)->where($where)->count();
        $this->smarty->assign('totalDataCount',$count);

        $limit = I('get.limit',10,'int');
        $Page = new Page($count,$limit);
        fetchPage($this->smarty,$Page);

        //查询数据
        $data = M('commerce_evaluate',null,DB_MAIN_CFG)
            ->where($where)
            ->order("evaluateTime DESC,objType ASC")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        foreach($data as $key => $val){
            $data[$key]['objType'] = EditorModel::getType($val['objType'])[0];
        }
        $this->smarty->assign('data',$data);
        $this->smarty->assign('type',$type);
        $this->smarty->assign('keyword',stripslashes($keyword));
        $this->smarty->assign('startDate',$startDate);
        $this->smarty->assign('endDate',$endDate);
        $this->smarty->assign('limit',$limit);
        $this->smarty->assign('title','商务评价统计');
        $this->smarty->display("Editor/EditorData/evaluate.html");
    }

    //微信好友统计
    public function weiXinFriendsStatistic(){
        $startDate = I('get.startDate','');
        $endDate = I('get.endDate','');
        $where = "1";

        $startTime = strtotime($startDate);
        $endTime = strtotime($endDate);
        if($startDate && $endDate){
            $startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
            $endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
            $where .= " and objTime between $startTime and $endTime";
        } else if($startDate){
            $startTime = mktime(0,0,0,date("m",$startTime),date("d",$startTime),date("Y",$startTime));
            $where .= " and objTime >= $startTime";
        } else if($endDate){
            $endTime = mktime(23,59,59,date("m",$endTime),date("d",$endTime),date("Y",$endTime));
            $where .= " and objTime <= $endTime";
        }

        $count = M('weixin_friend_statistic',null,DB_MAIN_CFG)->where($where)->count();
        $this->smarty->assign('totalDataCount',$count);

        $limit = I('get.limit',10,'int');
        $Page = new Page($count,$limit);
        fetchPage($this->smarty,$Page);

        //查询数据
        $data = M('weixin_friend_statistic',null,DB_MAIN_CFG)
            ->where($where)
            ->order("objTime DESC")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->smarty->assign('data',$data);
        $this->smarty->assign('startDate',$startDate);
        $this->smarty->assign('endDate',$endDate);
        $this->smarty->assign('limit',$limit);
        $this->smarty->assign('title','微信好友统计');
        $this->smarty->display("Editor/EditorData/weiXinFriends.html");

    }

}